package com.zuorenke.kafka;

import com.zuorenke.help.StringUtils;
import com.zuorenke.model.po.LogEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author Z.R.K
 * @description
 * @create 2019-04-09 09:05:44
 **/
@Component
public class LessonMessageListener {
	Logger logger = LoggerFactory.getLogger(LessonMessageListener.class);
	
//	@KafkaListener(topics = "${spring.kafka.modelTeacher.topics}", groupId = "${spring.kafka.modelTeacher.groupId}")
	public void processMessage(String msg) {
		LogEvent logEvent = msgToEvent(msg);
	}
	
	private LogEvent msgToEvent(String msg) {
		LogEvent logEvent = new LogEvent();
		try {
			String[] times = StringUtils.splitByChar(msg, ' ', 3);
			String[] keys = StringUtils.splitByChar(times[2], '`', 8);
			String param = keys[7];
			logEvent.setDate(times[0]);
			logEvent.setTime(times[1]);
			logEvent.setSessionId(Long.parseLong(keys[1]));
			logEvent.setLessonId(Long.parseLong(keys[2]));
			logEvent.setUserId(Long.parseLong(keys[3]));
			logEvent.setVersion(Integer.parseInt(keys[4]));
			logEvent.setClientType(Integer.parseInt(keys[5]));
			logEvent.setContentLength(Integer.parseInt(keys[6]));
			logEvent.setParam(param);
			int index = param.indexOf("bzCode\":");
			if (index != -1) {
				String temp = param.substring(index + 8);
				index = Math.min(temp.indexOf('}'), temp.indexOf(','));
				logEvent.setBzCode(Integer.parseInt(temp.substring(0, index)));
			} else {
				logEvent.setBzCode(0);
			}
		} catch (Exception e) {
			logger.error("解析日志失败 {} {}", msg, e);
			return null;
		}
		return logEvent;
	}
	
}
